<?php

class Db_Tables2_BLogsContract
{	
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetPaymentTime($contract_id_arr)
	{
		$result = array();
		
		if(is_array($contract_id_arr) && !empty($contract_id_arr))
		{
			$select = $this->dealers_db->select();
			$select->from("b_logs_contract", "*");
			$select->where("contract_id IN (?)", $contract_id_arr);
			$select->where("value > ?", 0);
			$select->order("ctime DESC");
			$rows = $this->dealers_db->fetchAll($select);
			
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					if(!isset($result[$row['contract_id']]))
					{
						$result[$row['contract_id']] = NULL;
					}
					
					$result[$row['contract_id']] = $row['ctime'];
				}
			}
		}
		
		return $result;
	}
	
	function GetPaymentAmountByMonth($contract_id)
	{
		$result = array();
		
		$select = $this->dealers_db->select();
		$select->from("b_logs_contract", array("DATE_FORMAT(ctime,'%Y年%c月') as dt", "sum(value) as vl"));
		$select->where("contract_id = ?", $contract_id);
		$select->group("dt");
		$rows = $this->dealers_db->fetchAll($select);
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				$result[$row['dt']] = $row['vl'];
			}
		}
		
		return $result;
	}
}